 package scripting.npc;

 import client.MapleClient;
 import configs.ServerConfig;
 import constants.ScriptType;
 import java.util.Map;
 import java.util.WeakHashMap;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import scripting.AbstractScriptManager;
 import tools.StringUtil;


 public class NPCScriptManager
   extends AbstractScriptManager
 {
/*  19 */   private static final Logger log = LogManager.getLogger(NPCScriptManager.class.getName());
/*  20 */   private static final NPCScriptManager instance = new NPCScriptManager();
/*  21 */   private final Map<MapleClient, NPCConversationManager> cms = new WeakHashMap<>();

   public static synchronized NPCScriptManager getInstance() {
/*  24 */     return instance;
   }

   public void start(MapleClient c, int npcId) {
/*  28 */     start(c, npcId, null);
   }
   public void start(MapleClient c, int npcId, String npcMode) {
     try {
       Invocable iv;
/*  33 */       if (this.cms.containsKey(c)) {
/*  34 */         dispose(c);

         return;
       }
/*  38 */       if (npcMode == null || npcMode.isEmpty()) {
/*  39 */         iv = getInvocable("npc/" + npcId + ".js", c, true);
/*  40 */       } else if (StringUtil.isNumber(npcMode)) {
/*  41 */         iv = getInvocable("npc/" + npcId + "_" + npcMode + ".js", c, true);
       } else {
/*  43 */         iv = getInvocable("extend/" + npcMode + ".js", c, true);
       }
/*  45 */       if (c.getPlayer().isAdmin()) {
/*  46 */         c.getPlayer().dropMessage(5, "开始NPC对话 NPC：" + npcId + " 模式：" + npcMode);
       }
/*  48 */       ScriptEngine scriptengine = (ScriptEngine)iv;
/*  49 */       NPCConversationManager cm = new NPCConversationManager(c, npcId, npcMode, ScriptType.NPC, iv);
/*  50 */       if (iv == null || getInstance() == null) {
/*  51 */         if (iv == null) {
/*  52 */           c.getPlayer().dropMessage(5, "找不到NPC脚本: " + npcId + " 模式: " + npcMode + " 当前所在地图: " + c.getPlayer().getMapId());
         }
/*  54 */         dispose(c);
         return;
       }
/*  57 */       this.cms.put(c, cm);
/*  58 */       scriptengine.put("cm", cm);
/*  59 */       c.getPlayer().setConversation(1);
/*  60 */       c.setClickedNPC();
       try {
/*  62 */         iv.invokeFunction("start", new Object[0]);
/*  63 */       } catch (NoSuchMethodException nsme) {
/*  64 */         iv.invokeFunction("action", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
       }
/*  66 */     } catch (Exception e) {
/*  67 */       log.error("执行NPC脚本出错 NPC ID : " + npcId + " 模式: " + npcMode + ".\r\n错误信息: ", e);
/*  68 */       dispose(c);
/*  69 */       notice(c, npcId, npcMode);
     }
   }

   public void action(MapleClient c, byte mode, byte type, int selection) {
/*  74 */     if (mode != -1) {
/*  75 */       NPCConversationManager cm = this.cms.get(c);
/*  76 */       if (cm == null) {
         return;
       }
       try {
/*  80 */         if (cm.pendingDisposal) {
/*  81 */           dispose(c);
         } else {
/*  83 */           c.setClickedNPC();
/*  84 */           cm.getIv().invokeFunction("action", new Object[] { Byte.valueOf(mode), Byte.valueOf(type), Integer.valueOf(selection) });
         }
/*  86 */       } catch (Exception e) {
/*  87 */         int npcId = cm.getNpc();
/*  88 */         String npcMode = cm.getNpcMode();
/*  89 */         log.error("执行NPC脚本出错 NPC ID : " + npcId + " 模式: " + npcMode + ". \r\n错误信息: ", e);
/*  90 */         dispose(c);
/*  91 */         notice(c, npcId, npcMode);
       }
     }
   }

   public void dispose(MapleClient c) {
/*  97 */     NPCConversationManager npccm = this.cms.get(c);
/*  98 */     StringBuilder stringBuilder = new StringBuilder();
/*  99 */     if (npccm != null) {
/* 100 */       this.cms.remove(c);

/* 102 */       if (null != npccm.getType()) {
/* 103 */         switch (npccm.getType()) {
           case NPC:
/* 105 */             if (npccm.getScript() == null) {
/* 106 */               stringBuilder.append(ServerConfig.WORLD_SCRIPTSPATH).append("/npc/").append(npccm.getNpc()).append(".js");
/* 107 */               c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/npc/" + npccm.getNpc() + ".js"); break;
/* 108 */             }  if (StringUtil.isNumber(npccm.getScript())) {
/* 109 */               stringBuilder.append(ServerConfig.WORLD_SCRIPTSPATH).append("/npc/").append(npccm.getNpc()).append("_").append(npccm.getScript()).append(".js");
/* 110 */               c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/npc/" + npccm.getNpc() + "_" + npccm.getScript() + ".js"); break;
             }
/* 112 */             stringBuilder.append(ServerConfig.WORLD_SCRIPTSPATH).append("/extend/").append(npccm.getScript()).append(".js");
/* 113 */             c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/extend/" + npccm.getScript() + ".js");
             break;


           case ON_USER_ENTER:
/* 118 */             stringBuilder.append(ServerConfig.WORLD_SCRIPTSPATH).append("/map/onUserEnter/").append(npccm.getScript()).append(".js");
             break;

           case ON_FIRST_USER_ENTER:
/* 122 */             stringBuilder.append(ServerConfig.WORLD_SCRIPTSPATH).append("/map/onFirstUserEnter/").append(npccm.getScript()).append(".js");
             break;
         }

       }
/* 127 */       c.removeScriptEngine(stringBuilder.toString());
     }
/* 129 */     if (c.getPlayer() != null && c.getPlayer().getConversation() == 1) {
/* 130 */       c.getPlayer().setConversation(0);
     }
   }

   public NPCConversationManager getCM(MapleClient c) {
/* 135 */     return this.cms.get(c);
   }

   private void notice(MapleClient c, int npcId, String npcMode) {
/* 139 */     c.getPlayer().dropMessage(1, "这个NPC脚本是错误的，请联系管理员修复它.NPCID: " + npcId + ((npcMode != null && !npcMode.isEmpty()) ? (" 模式:" + npcMode) : ""));
   }

   public final void onUserEnter(MapleClient c, String scriptname) {
     try {
/* 144 */       if (c.getPlayer().isAdmin()) {
/* 145 */         c.getPlayer().dropMessage(5, "[地图脚本] 执行onUserEnter脚本：" + scriptname + " 地图：" + c.getPlayer().getMap().getId() + " - " + c.getPlayer().getMap().getMapName());
       }
/* 147 */       Invocable iv = getInvocable("map/onUserEnter/" + scriptname + ".js", c, true);
/* 148 */       ScriptEngine scriptEngine = (ScriptEngine)iv;
/* 149 */       NPCConversationManager ms = new NPCConversationManager(c, 0, scriptname, ScriptType.ON_USER_ENTER, iv);
/* 150 */       if (this.cms.containsValue(ms)) {
/* 151 */         if (c.getPlayer().isAdmin()) {
/* 152 */           c.getPlayer().dropMessage(5, "无法执行脚本:已有脚本执行 - " + this.cms.containsKey(c));
         }
/* 154 */         dispose(c);
         return;
       }
/* 157 */       if (iv == null || getInstance() == null) {
/* 158 */         if (iv == null);


/* 161 */         dispose(c);
         return;
       }
/* 164 */       this.cms.put(c, ms);
/* 165 */       scriptEngine.put("ms", ms);
/* 166 */       c.getPlayer().setConversation(1);
/* 167 */       c.setClickedNPC();
       try {
/* 169 */         iv.invokeFunction("start", new Object[0]);
/* 170 */       } catch (NoSuchMethodException nsme) {
/* 171 */         iv.invokeFunction("action", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
       }
/* 173 */     } catch (NoSuchMethodException|javax.script.ScriptException exception) {
/* 174 */       log.error("执行地图onUserEnter脚本出錯 : " + scriptname + ".\r\n错误信息：", exception);
/* 175 */       dispose(c);
     }
   }


   public final void onFirstUserEnter(MapleClient c, String scriptname) {
     try {
/* 182 */       if (c.getPlayer().isAdmin()) {
/* 183 */         c.getPlayer().dropMessage(5, "[地图脚本] 执行onFirstUserEnter脚本：" + scriptname + " 地图：" + c.getPlayer().getMap().getId() + " - " + c.getPlayer().getMap().getMapName());
       }
/* 185 */       if (this.cms.containsKey(c)) {
/* 186 */         if (c.getPlayer().isAdmin()) {
/* 187 */           c.getPlayer().dropMessage(5, "无法执行脚本:已有脚本执行 - " + this.cms.containsKey(c));
         }
/* 189 */         dispose(c);
         return;
       }
/* 192 */       Invocable iv = getInvocable("map/onFirstUserEnter/" + scriptname + ".js", c, true);
/* 193 */       ScriptEngine scriptEngine = (ScriptEngine)iv;
/* 194 */       NPCConversationManager ms = new NPCConversationManager(c, 0, scriptname, ScriptType.ON_FIRST_USER_ENTER, iv);
/* 195 */       if (iv == null || getInstance() == null) {
/* 196 */         if (iv != null || c.getPlayer().isAdmin());


/* 199 */         dispose(c);
         return;
       }
/* 202 */       this.cms.put(c, ms);
/* 203 */       scriptEngine.put("ms", ms);
/* 204 */       c.getPlayer().setConversation(1);
/* 205 */       c.setClickedNPC();
       try {
/* 207 */         iv.invokeFunction("start", new Object[0]);
/* 208 */       } catch (NoSuchMethodException nsme) {
/* 209 */         iv.invokeFunction("action", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
       }
/* 211 */     } catch (NoSuchMethodException|javax.script.ScriptException exception) {
/* 212 */       log.error("onFirstUserEnter : " + scriptname + ".\r\n错误信息：", exception);
/* 213 */       dispose(c);
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\npc\NPCScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */